package com.infindo.appcreate.zzyj.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.infindo.appcreate.zzyj.entity.SysUser;

public class AccessControlInterceptor extends HandlerInterceptorAdapter {
	static String[] frontKeywords = new String[] { "login" };
	static String[] mobiKeywords = new String[] { "noPriviledgePage", "error" };

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String root = request.getContextPath();
		String url = request.getRequestURL().toString();

		boolean handlerOk = super.preHandle(request, response, handler);
		if (handlerOk) {

			// For statistics
			// if (url.indexOf("/mobi/") > 0) {
			// CltClient client = (CltClient)
			// request.getSession().getAttribute("currentClient");
			// if (client == null) {
			// wrapRequestInfo(request);
			// }
			// }

			if (url.indexOf("/front") > 0) {
				if (checkKeywords(url, frontKeywords)) {
					return true;
				}
				HttpSession session = request.getSession();
				SysUser client = (SysUser) session
						.getAttribute("currentClient");
				if (client != null) {
					return true;
				} else {
					response.sendRedirect(root + "/front/login");
					return false;
				}
			} else {
				return true;
			}
		}
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object o, ModelAndView mv)
			throws Exception {
		String root = request.getContextPath();
		String url = request.getRequestURL().toString();

		boolean isLogin = false;
		boolean hasApp = false;
		boolean live = false;

		// if (url.indexOf("/mobi") > 0) {
		// if (checkKeywords(url, mobiKeywords)) {
		// return;
		// }
		// HttpSession session = request.getSession();
		// CltClient client = (CltClient) session
		// .getAttribute("currentClient");
		// if (client != null) {
		// isLogin = true;
		// }
		//
		// AppApplication app = (AppApplication) session
		// .getAttribute("appApplication");
		// if (app != null) {
		// hasApp = true;
		// if (app.getStatus() != null
		// && app.getStatus().equals(
		// StatusEnum.App.Live.toString())) {
		// live = true;
		// }
		// }
		//
		// if (!hasApp || !(isLogin || live)) {
		// response.sendRedirect(root + "/mobi/noPriviledgePage");
		// }
		// }
	}

	private boolean checkKeywords(String url, String[] frontKeywords) {
		String temp = url.toLowerCase();
		for (String word : frontKeywords) {
			if (temp.indexOf(word.toLowerCase()) > 0) {
				return true;
			}
		}
		return false;
	}

	// private String getWholeUrl(HttpServletRequest request) {
	// StringBuffer sb = request.getRequestURL();
	// StringBuffer parameters = new StringBuffer();
	// Enumeration<String> attrNames = request.getParameterNames();
	// while (attrNames.hasMoreElements()) {
	// String attrName = attrNames.nextElement();
	// parameters.append(attrName);
	// parameters.append("=");
	// parameters.append(request.getParameter(attrName));
	// parameters.append("&");
	// }
	// String paramStr = parameters.toString();
	// if (StringUtils.isNotEmpty(paramStr)) {
	// paramStr = "?" + paramStr.substring(0, parameters.length() - 1);
	// return sb.toString() + paramStr;
	// } else {
	// return sb.toString();
	// }
	// }
}